package chapter01;

import java.util.Comparator;

public class FindMaxDemo {

    /**
     * Return max item in arr.
     * Procondition: arr.length > 0
     * @param arr
     * @return
     */
    public static Comparable findMax(Comparable[] arr) {
        int maxIndex = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].compareTo(arr[maxIndex]) > 0) {
                maxIndex = i;
            }
        }
        return arr[maxIndex];
    }


    public static <AnyType> AnyType findMax3(AnyType[] arr, Comparator<? super AnyType> comparator) {
        int maxIndex = 0;

        for (int i = 0; i < arr.length; i++) {
            if (comparator.compare(arr[i], arr[maxIndex]) > 0) {
                maxIndex = i;
            }
        }
        return arr[maxIndex];
    }

    /**
     * Test finMax on Shape and String objects.
     * @param args
     */
    public static void main(String[] args) {
        Shape[] shapes = {new Circle(2.0), new Square(3.0), new Rectangle(3.0, 4.0)};

        String[] shapes2 = {"Joe", "Bob", "Bill", "Zeke"};

        System.out.println(findMax(shapes));
        System.out.println(findMax(shapes2));
    }


    /* 编写一个程序求解字谜游戏问题。
     */
    static class Z_Pra_1_2{
        public static final String[] WORDS={"this","two","fat","that"};
        public static final char[][] ziMi={{'t','h','i','s'},{'w','a','t','s'},{'o','a','h','g'},{'f','g','d','t'}};

        public static void main(String[] args){
            findWords(ziMi, WORDS);
        }

        // findWords----(x,y)----( ↑↓ →← ↗↙ ↘↖ )
        public static void findWords(char[][] arr,String[] words) {
            String initials="";//记录首字母,不重复
            for(String word:words){
                String initial=word.substring(0, 1);
                if(initials.contains(initial)==false){
                    initials+=initial;
                }
            }//遍历每个元素是否是首字母
            for(int x=0;x<arr.length;x++){
                for(int y=0;y<arr[x].length;y++){
                    if(!initials.contains(String.valueOf(arr[x][y])))continue;
                    else{
                        for(int p=1;p<=8;p++){
                            String tem="";
                            int xEnd=x,yEnd=y;
                            while(xyAva(arr, xEnd, yEnd)){
                                tem+=arr[xEnd][yEnd];
                                printWord(tem, x, y, xEnd, yEnd);
                                if(p==1)xEnd--;                        //↑
                                if(p==2)xEnd++;                        //↓
                                if(p==3)yEnd++;                        //→
                                if(p==4)yEnd--;                        //←
                                if(p==5){xEnd--;yEnd++;}               //↗
                                if(p==6){xEnd++;yEnd--;}               //↙
                                if(p==7){xEnd++;yEnd++;}               //↘
                                if(p==8){xEnd--;yEnd--;}               //↖
                            }
                        }
                    }
                }
            }
        }

        //(x,y)available
        public static boolean xyAva(char[][] arr,int x,int y){
            if(0<=x&&x<arr.length){
                if(0<=y&&y<arr[x].length)return true;
            }
            return false;
        }

        // 判断是否是给定的一个单词并打印
        public static void printWord(String word,int x,int y,int xEnd,int yEnd) {
            for (int m=0;m<WORDS.length;m++){
                if (word.equals(WORDS[m])){
                    System.out.println(WORDS[m]+"\tscalar:"+"("+(x+1)+","+(y+1)+")->("+(xEnd+1)+"," +(yEnd+1)+")");
                }
            }
        }
    }
}




